﻿using System.ComponentModel.DataAnnotations.Schema;

namespace Simple.Repository.Models.System;

[Table("QHSE_XY_SYSUSERDATASCOPE")]
public class SysUserDataScope : EntityBase
{

    /// <summary>
    /// 用户Id
    /// </summary>
    public string UserId { get; set; }

    /// <summary>
    /// 组织Id
    /// </summary>
    public string OrganizationId { get; set; }

    /// <summary>
    /// 用户
    /// </summary>
    public SysUser? User { get; set; }

    public override void ConfigureEntity(ModelBuilder builder)
    {
        // 复合主键
        builder.Entity<SysUserDataScope>()
            .HasKey(e => new { e.UserId, e.OrganizationId });

        // Index
        builder.Entity<SysUserDataScope>()
            .HasIndex(e => new { e.OrganizationId })
            .IsUnique(false);

        // 关系配置
        builder.Entity<SysUserDataScope>()
            .HasOne(ud => ud.User)
            .WithMany(r => r.UserDataScopes)
            .IsRequired();
    }
}
